package md.mru.frontend.controllers;

import md.mru.core.dtos.*;
import md.mru.core.service.EmploymentDataService;
import org.primefaces.context.RequestContext;
import org.primefaces.event.RowEditEvent;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sergiu.marin
 * @since 29.03.2014
 */
@ManagedBean(name = "employmentDataBean")
@ViewScoped
public class EmploymentDataAdministrationBean extends BackingBean {

    private static final long serialVersionUID = 3674781217281272940L;

    @ManagedProperty(value = "#{employmentDataService}")
    private EmploymentDataService dataService;
    @ManagedProperty(value = "#{folderAdministrationBean}")
    private FolderAdministrationBean folderAdministrationBean;

    private List<ContractDto> contracts = new ArrayList<>();
    private List<WorkExperienceDto> workExperiences = new ArrayList<>();
    private List<WorkTypeDto> workCategories = new ArrayList<>();
    private List<SalaryDto> salaries = new ArrayList<>();
    private List<SalaryLevelDto> salaryLevels = new ArrayList<>();
    private List<SalaryStepDto> salarySteps = new ArrayList<>();
    private ContractDto selectedContract;
    private ContractAgreementDto selectedContractAgreement;
    private WorkExperienceDto selectedWorkExperience;
    private SalaryDto selectedSalary;

    public void initEmploymentDataTab() {
        contracts = dataService.getContracts(this.folderAdministrationBean.getSelectedFolder().getId());
    }

    public void initSalaryDialog() {
        if (this.salaryLevels.isEmpty()) {
            this.salaryLevels = dataService.getSalaryLevels();
            this.getObjectMappingConverter().mapObjectList(salaryLevels);
        }
        if (this.salarySteps.isEmpty()) {
            this.salarySteps = dataService.getSalarySteps();
            this.getObjectMappingConverter().mapObjectList(salarySteps);
        }
    }

    public void resetCreateSalaryDialog() {
        this.selectedSalary = new SalaryDto();
        this.initSalaryDialog();
    }

    public void initEditSalaryDialog(SalaryDto salary) {
        this.selectedSalary = salary;
        this.initSalaryDialog();
    }

    public void resetCreateContractDialog() {
        this.selectedContract = new ContractDto();
        this.selectedContract.setFolder(this.folderAdministrationBean.getSelectedFolder().getId());
    }

    public void resetCreateContractAgreementDialog() {
        this.selectedContractAgreement = new ContractAgreementDto();
        this.selectedContractAgreement.setContract(selectedContract);
    }

    public void resetCreateWorkExperienceDialog() {
        this.selectedWorkExperience = new WorkExperienceDto();
        this.selectedWorkExperience.setFolder(this.folderAdministrationBean.getSelectedFolder().getId());
        this.initWorkExperienceDialog();
    }

    public void initWorkExperienceDialog() {
        if (this.workCategories.isEmpty()) {
            this.workCategories = dataService.getWorkCategories();
            this.getObjectMappingConverter().mapObjectList(this.workCategories);
        }
    }

    public void initEditWorkExperienceDialog(WorkExperienceDto workExperience) {
        this.selectedWorkExperience = workExperience;
        this.initWorkExperienceDialog();
    }


    /*public void addNewContract(String editRowId) {
        ContractDto c = new ContractDto();
        contracts.add(c);
        RequestContext.getCurrentInstance().execute(this.constructOnCompleteScript("ctc-formId", "ctcData", editRowId));
    }*/

    public void addNewWorkExperience(String editRowId) {
        WorkExperienceDto w = new WorkExperienceDto();
        workExperiences.add(w);
        RequestContext.getCurrentInstance().execute(this.constructOnCompleteScript("we-formId", "weData", editRowId));
    }

    public void addNewSalary(String editRowId) {
        SalaryDto sr = new SalaryDto();
        salaries.add(sr);
        RequestContext.getCurrentInstance().execute(this.constructOnCompleteScript("sr-formId", "srData", editRowId));
    }

    public void saveContract() {
        long id = dataService.saveContract(selectedContract);
        if (selectedContract.getId() == 0) {
            selectedContract.setId(id);
            contracts.add(selectedContract);
        }
    }

    public void saveSalary() {
        long id = dataService.saveSalary(selectedSalary);
        if (selectedSalary.getId() == 0) {
            selectedSalary.setId(id);
            salaries.add(selectedSalary);
        }
    }

    public void saveContractAgreement() {
        long id = dataService.saveContractAgreement(selectedContractAgreement);
        if (selectedContractAgreement.getId() == 0) {
            selectedContractAgreement.setId(id);
            selectedContract.getContractAgreements().add(selectedContractAgreement);
        }
    }

    public void saveWorkExperience() {
        long id = dataService.saveWorkExperience(selectedWorkExperience);
        if (selectedWorkExperience.getId() == 0) {
            selectedWorkExperience.setId(id);
            workExperiences.add(selectedWorkExperience);
        }
    }

    public void onContractDataEdit(RowEditEvent event) {
        ContractDto contract = ((ContractDto) event.getObject());
        contract.setId(dataService.saveContract(contract));
    }

    public void onContractAgreementDataEdit(RowEditEvent event) {
        ContractAgreementDto contractAgreement = ((ContractAgreementDto) event.getObject());
        contractAgreement.setId(dataService.saveContractAgreement(contractAgreement));
    }

    public void onWorkExperienceDataEdit(RowEditEvent event) {
        WorkExperienceDto workExperience = ((WorkExperienceDto) event.getObject());
        workExperience.setId(dataService.saveWorkExperience(workExperience));
    }

    public void onSalaryDataEdit(RowEditEvent event) {
        SalaryDto salary = ((SalaryDto) event.getObject());
        salary.setId(dataService.saveSalary(salary));
    }

    public void deleteContract(ContractDto c) {
        dataService.deleteContract(c);
        this.contracts.remove(c);
    }

    public void deleteContractAgreement(ContractAgreementDto c) {
        dataService.deleteContractAgreement(c);
        this.selectedContract.getContractAgreements().remove(c);
    }

    public void deleteWorkExperience(WorkExperienceDto w) {
        dataService.deleteWorkExperience(w);
        this.workExperiences.remove(w);
    }

    public void deleteSalary(SalaryDto sr) {
        dataService.deleteSalary(sr);
        this.salaries.remove(sr);
    }


    public List<ContractDto> getContracts() {
        return contracts;
    }

    public void setContracts(List<ContractDto> contracts) {
        this.contracts = contracts;
    }

    public List<WorkExperienceDto> getWorkExperiences() {
        return workExperiences;
    }

    public void setWorkExperiences(List<WorkExperienceDto> workExperiences) {
        this.workExperiences = workExperiences;
    }

    public List<WorkTypeDto> getWorkTypes() {
        return workCategories;
    }

    public void setWorkCategories(List<WorkTypeDto> workCategories) {
        this.workCategories = workCategories;
    }

    public List<SalaryDto> getSalaries() {
        return salaries;
    }

    public void setSalaries(List<SalaryDto> salaries) {
        this.salaries = salaries;
    }

    public List<SalaryLevelDto> getSalaryLevels() {
        return salaryLevels;
    }

    public void setSalaryLevels(List<SalaryLevelDto> salaryLevels) {
        this.salaryLevels = salaryLevels;
    }

    public List<SalaryStepDto> getSalarySteps() {
        if (salarySteps.isEmpty()) {
            salarySteps = new ArrayList<>(10);
            for (int i = 1; i <= 10; i++) {
                SalaryStepDto ss = new SalaryStepDto();
                ss.setId(i);
                ss.setName("Salary_step_" + 1);
                salarySteps.add(ss);
            }
            getObjectMappingConverter().mapObjectList(salarySteps);
        }
        return salarySteps;
    }

    public void setSalarySteps(List<SalaryStepDto> salarySteps) {
        this.salarySteps = salarySteps;
    }

    public void setDataService(EmploymentDataService dataService) {
        this.dataService = dataService;
    }

    public ContractDto getSelectedContract() {
        return selectedContract;
    }

    public void setSelectedContract(ContractDto selectedContract) {
        this.selectedContract = selectedContract;
    }

    public ContractAgreementDto getSelectedContractAgreement() {
        return selectedContractAgreement;
    }

    public void setSelectedContractAgreement(ContractAgreementDto selectedContractAgreement) {
        this.selectedContractAgreement = selectedContractAgreement;
    }

    public WorkExperienceDto getSelectedWorkExperience() {
        return selectedWorkExperience;
    }

    public void setSelectedWorkExperience(WorkExperienceDto selectedWorkExperience) {
        this.selectedWorkExperience = selectedWorkExperience;
    }

    public void setFolderAdministrationBean(FolderAdministrationBean folderAdministrationBean) {
        this.folderAdministrationBean = folderAdministrationBean;
    }

    public SalaryDto getSelectedSalary() {
        return selectedSalary;
    }

    public void setSelectedSalary(SalaryDto selectedSalary) {
        this.selectedSalary = selectedSalary;
    }
}
